<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        首页配置
      </div>

      <parser v-if="formConfChild.render" :is-edit="formConfChild.isEdit" :form-conf="formConfChild.content"
        :form-edit-data="currentEditData" @submit="handlerSubmit" />
    </el-card>
  </div>
</template>

<script>
  //用于一些固定字段的表单页面 直接复制，修改id即可
  import * as systemFormConfigApi from '@/api/systemFormConfig.js'
  import * as systemSettingApi from '@/api/systemSetting.js'
  import * as systemConfigApi from '@/api/systemConfig.js'
  import {
    Debounce
  } from '@/utils/validate'
  import parser from '@/components/FormGenerator/components/parser/Parser'
  export default {
    components: {
      parser
    },
    data() {
      return {
        loading: false,
        currentEditData: {},
        formConf: {
          content: {
            fields: []
          },
          id: null,
          render: false,
          isEdit: false
        },
        formConfChild: {
          content: {
            fields: []
          },
          id: null,
          render: false,
          isEdit: false
        },
        currentEditId: ""
      }
    },

    mounted() {
      this.handlerGetLevel2FormConfig(147);
    },
    methods: {
      handlerGetLevel2FormConfig(id) {
        const formPram = {
          id: id
        }
        this.formConfChild.content = {
          fields: []
        }
        this.formConfChild.render = false
        this.loading = true
        systemFormConfigApi.getFormConfigInfo(formPram).then(data => {
          const {
            id,
            name,
            info,
            content
          } = data
          this.formConfChild.content = JSON.parse(content)
          this.formConfChild.id = id
          this.handlerGetSettingInfo(id, 2)
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      },
      handlerGetSettingInfo(id, level) {
        this.currentEditId = id
        systemSettingApi.systemConfigInfo({
          id: id
        }).then(data => {
          this.currentEditData = data
          if (level === 1) {
            this.formConf.isEdit = this.currentEditData !== null
            this.formConf.render = true
          } else {
            this.formConfChild.isEdit = this.currentEditData !== null
            this.formConfChild.render = true
          }
        })
      },

      handlerSubmit: Debounce(function(formValue) {
        this.handlerSave(formValue)
      }),

      handlerSave(formValue) {
        const _pram = this.buildFormPram(formValue)
        let _formId = 0
        systemSettingApi.systemConfigSave(_pram).then(data => {
          this.$message.success('添加数据成功')
        })
      },

      buildFormPram(formValue) {
        const _pram = {
          fields: [],
          id: this.currentEditId,
          sort: 0, // 参数暂时无用
          status: true // 参数暂时无用
        }
        const _fields = []
        Object.keys(formValue).forEach((key) => {
          _fields.push({
            name: key,
            title: key,
            value: formValue[key]
          })
        })
        _pram.fields = _fields
        return _pram
      },

    }
  }
</script>

<style lang="sass" scoped>

</style>
